package com.lbd.util;

import org.apache.log4j.Logger;

import java.io.IOException;

/**
 * Created by yuanbo on 2016/10/14.
 */
public class StoreUtil {
    private static final Logger log = Logger.getLogger(StoreUtil.class);

    /**
     * 存储文件工具类
     *
     * @param suffix      后缀名
     * @param inputStream 文件流
     * @return 文件存储位置
     *//*
    public static String store(String suffix, InputStream inputStream) {
        String path = getFilePath(suffix);
        String fileName = null;
        try {
            fileName = writeFile(readFile(inputStream), path);
        } catch (IOException e) {
            log.info("存储文件失败" + e);
        }
        fileName = PathFormat.format(fileName);
        return fileName;
    }

    *//*
     * 读取源文件内容
     * @param inputStream String 文件路径
     * @throws IOException
     *//*
    public static byte[] readFile(InputStream inputStream) throws IOException {
        byte[] bytes = new byte[1024];
        int leng;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((leng = inputStream.read(bytes)) != -1) {
            baos.write(bytes, 0, leng);
        }
        return baos.toByteArray();
    }

    *//**
     * 将数据写入文件
     *
     * @param data byte[]
     * @throws IOException
     *//*
    public static String writeFile(byte[] data, String filename) throws IOException {
        File file = new File(filename);
        file.getParentFile().mkdirs();
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
        bufferedOutputStream.write(data);
        bufferedOutputStream.close();
        return filename;
    }

    private static String getFilePath(String suffix) {
        //// TODO: 2016/10/14  文件存储路径
        StringBuilder fileName = new StringBuilder("E://test");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat timeFormat = new SimpleDateFormat("ddHHmm");
        fileName = fileName.append("\\" + dateFormat.format(new Date()) + "\\");
        fileName = fileName.append(timeFormat.format(new Date()));
        fileName = fileName.append(com.baidu.ueditor.upload.ShortUUID.generateShortUuid());
        fileName = fileName.append("." + suffix);
        return fileName.toString();
    }*/

}
